package com.zeta.eduservice.controller.question;


import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.servlets.CGIServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.question.EduChapter;
import com.zeta.eduservice.entity.vo.ChapterVo;
import com.zeta.eduservice.entity.vo.node.TypesysNode;
import com.zeta.eduservice.entity.vo.node.chapterNode;
import com.zeta.eduservice.service.EduChapterService;
import com.zeta.exceptionhandler.GuliException;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-08-02
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu_chapter/")
public class EduChapterController {

	@Autowired
	private EduChapterService chapterService;
	
	@ApiOperation("添加")
	@PostMapping("")
	public R add(@RequestBody EduChapter chapter) {
		chapterService.save(chapter);
		return R.ok();
		
	}
	@ApiOperation("删除啊")
	@DeleteMapping("{chapterId}")
	public R delete(@PathVariable String chapterId) {
		chapterService.deleteChapter(chapterId);
		return R.ok();
		
	}
	@ApiOperation("修改啊")
	@PutMapping("")
	public R update(@RequestBody EduChapter chapter) {
		boolean  re = chapterService.updateById(chapter);
		if(re==false) {
			throw new GuliException(20001,"更新失败");
		}
		return R.ok();
		
	}
	@ApiOperation("根据章节id查")
	@GetMapping("{chapterId}")
	public R getByChapterId(@PathVariable String chapterId) {
		EduChapter byId = chapterService.getById(chapterId);
		return R.ok().data("chapter",byId);
	}
	
	
	@ApiOperation(value = "章节数据列表")
	@GetMapping("findCourseList/{courseId}")
	public R nestedListByCourseId(
			@ApiParam(name = "courseId", value = "课程ID", required = true)
			@PathVariable String courseId){
		
		List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
		return R.ok().data("chapters", chapterVoList);
	}
	@GetMapping("getChapterAndInfo/{courseId}")
	public R getChapterAndInfo(@PathVariable String courseId) {
		ArrayList<chapterNode> chapterAndInfo = chapterService.getChapterAndInfo(courseId);
		return R.ok().data("list",chapterAndInfo);
		
	}
	
	
}

